﻿using EntGuayasSLA.BL;
using EntGuayasSLA.BL.Listas;
using EntGuayasSLA.BL.Listas.Criterias;
using Ext.Net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EGuayas.Web.Licenciamiento.promotor.tabs
{
    public partial class wucCostos : ucWuc
    {
       
        protected String PageId
        {
            get
            {
                return ReadCustomConfigPage(this.pnlCostos, this.ID);
            }

        }
       
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!X.IsAjaxRequest)
                {
                       string idExpedienteLicencia = GetSessionPage.ExpedienteLic(SessionPage.PageLicPro);
                       float rounded = 0f;
                       float sum = 0f;
                       DetalleCostoLista listaIems = DetalleCostoLista.GetDetalleCostoLista(idExpedienteLicencia);
                       this.stoCostos.DataSource   = listaIems;
                       this.stoCostos.DataBind();

                       if (listaIems.Count != 0)
                       {
                           ExpedienteLicenciamiento objExpLic = ExpedienteLicenciamiento.GetEditableRoot(Guid.Parse(idExpedienteLicencia));
                           foreach (InfoDetalleCosto val in listaIems)
                           {
                               sum +=  GetValInfo(val.Valor);
                           }

                           rounded =objExpLic.Tasa/ sum ;
                           this.txtValorTasa.SetValue(rounded);
                          
                           rounded = objExpLic.Inspeccion;
                           this.txtIspec.Text = rounded.ToString();
                           rounded = objExpLic.Seguimiento;
                           this.txtSegu.Text = rounded.ToString();

                           X.Js.Call("wucJsCostos.totalCost", listaIems);

                       }
                      

                }
            }
            catch (Exception ex)
            {
                RegistrarLog("wucCostos.Page_Load" + ex);
            }
        }

        private  float GetValSettings(string valId)
        {
            float rounded = 0f;
            float.TryParse(ConfigurationManager.AppSettings.Get(valId), out rounded);
            float.TryParse(string.Format("{0:n2}", (Math.Truncate(rounded * 100) / 100)), out rounded);
            return rounded;
        }
        private float GetValInfo(string valData)
        {
            float rounded = 0f;
            float.TryParse(valData, out rounded);
            float.TryParse(string.Format("{0:n2}", (Math.Truncate(rounded * 100) / 100)), out rounded);          
            return rounded;
        }
       
    }
}